package com.atguigu.yygh.hosp.service.impl;

import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.hosp.repository.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl  implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private DictFeignClient dictFeignClient;


    @Override
    public void saveHospital(Hospital hospital) {

        //处理LogoData的数据问题,经过网络传输后"+"转换为了""
        //将""转换为"+"

        String s = hospital.getLogoData().replaceAll(" ", "+");
        hospital.setLogoData(s);

        //判断DB中是否存在该医院
        Hospital hospitalDB=hospitalRepository.findByHoscode(hospital.getHoscode());

        if(hospitalDB==null){

            //mongoDB 中不存在    执行添加

            //设置未上传的字段
            hospital.setStatus(0);
            hospital.setIsDeleted(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
        }else{
            //执行修改

            //设置条件ID
            hospital.setId(hospitalDB.getId());

            //设置未上传的字段
            hospital.setStatus(hospital.getStatus());
            hospital.setIsDeleted(hospital.getIsDeleted());
            hospital.setCreateTime(hospital.getCreateTime());
            hospital.setUpdateTime(new Date());
        }
        hospitalRepository.save(hospital);

    }

    @Override
    public Hospital getHospital(String hoscode) {

        Hospital hospital = hospitalRepository.findByHoscode(hoscode);

        return hospital;
    }

    @Override
    public Map<String, Object> pageList(HospitalQueryVo hospitalQueryVo, Integer page, Integer size) {

        //组装查询条件
        Criteria criteria = new Criteria();
        Hospital hospital = new Hospital();
        //省的编号
        String provinceCode = hospitalQueryVo.getProvinceCode();
        if(!StringUtils.isEmpty(provinceCode)){
            criteria.and("provinceCode").is(provinceCode);
        }
        String cityCode = hospitalQueryVo.getCityCode();
        if(!StringUtils.isEmpty(cityCode)){
            criteria.and("cityCode").is(cityCode);
        }
        String hosname = hospitalQueryVo.getHosname();
        if(!StringUtils.isEmpty(hosname)){
            criteria.and("hosname").regex(hosname);
        }

        String hostype = hospitalQueryVo.getHostype();
        if(!StringUtils.isEmpty(hostype)){
            criteria.and("hostype").is(hostype);
        }

        String districtCode = hospitalQueryVo.getDistrictCode();
        if(!StringUtils.isEmpty(districtCode)){
            criteria.and("districtCode").is(districtCode);
        }

        //最终的查询条件
        Query query = new Query(criteria);

        //查询数量
        long count = mongoTemplate.count(query, Hospital.class);

        //分页参数
        query.skip((page-1)*size).limit(size);



        //查询列表
        List<Hospital> hospitalList = mongoTemplate.find(query, Hospital.class);

        //数据转换  将编号转换为具体的医院等级,省市区.遍历
        hospitalList.forEach(this::handlerName);


        //封装数据
        Map<String, Object> map = new HashMap<>();
        map.put("hospitalList",hospitalList);
        map.put("count",count);
        return map;


    }

    @Override
    public void updateStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());

        hospitalRepository.save(hospital);
    }

    @Override
    public Hospital getById(String id) {
         Hospital hospital=hospitalRepository.findById(id).get();
         handlerName(hospital);
         return hospital;
    }

    @Override
    public List<Hospital> getListByName(String hosname) {

        return hospitalRepository.findByHosnameLike(hosname);
    }

    @Override
    public Hospital getByCode(String hoscode) {
        Hospital hospital=hospitalRepository.findByHoscode(hoscode);
        handlerName(hospital);
        return hospital;
    }

    private void handlerName(Hospital hospital) {
        String hostype = hospital.getHostype();
        String provinceCode = hospital.getProvinceCode();
        String cityCode = hospital.getCityCode();
        String districtCode = hospital.getDistrictCode();

        //医院等级
        String hostypeName=dictFeignClient.getByValueAndCode(Long.parseLong(hostype), DictEnum.HOSTYPE.getDictCode());


        //省市区
        String provinceName = dictFeignClient.findNameByValue(Long.parseLong(provinceCode));
        String cityName = dictFeignClient.findNameByValue(Long.parseLong(cityCode));
        String districtName = dictFeignClient.findNameByValue(Long.parseLong(districtCode));

        //拼接出详细地址
        String fullAddress=provinceName+cityName+districtName+hospital.getAddress();

        //封装到实体类param中
        hospital.getParam().put("hostypeName",hostypeName);
        hospital.getParam().put("fullAddress",fullAddress);

    }
}
